<template>
	<view></view>
</template>

<script>
	import {
		launchMiniProgram,
		isIOS
	} from '../../utils/share.js';

	import {
		getConfigRecycleBag
	} from '@/common/config.js';

	export default {
		props: {
			bagEqCode: String,
			checkHasTask: {
				type: Boolean,
				default () {
					return true;
				}
			},
			// 如有任务，直接运行任务
			runTaskDirectly: {
				type: Boolean,
				default () {
					return false;
				}
			},
			showErrTip: {
				type: Boolean,
				default () {
					return false;
				}
			}
		},
		data() {
			return {
				thirdConfigList: [], //配置信息
				isStatus: false,
				qcPlay: "",
				subscribeMpTaskUseOnline: true,
				subscribeMpTask: false,
				getConfig: false,
				subscribeMpTaskGotoMode: null
			};
		},
		created() {
			this.getConfigRecycleBag();

			if (this.checkHasTask) {
				this.getActivityStatus()
			}
			if (uni.getStorageSync("qcPlay")) {
				this.qcPlay = uni.getStorageSync("qcPlay")
			}
		},
		computed: {
			hasSubscribeMpTask() {
				if (this.subscribeMpTaskUseOnline) {
					return this.subscribeMpTask;
				} else {
					return this.qcPlay && this.qcPlay.isShow && !this.qcPlay.dataValid;
				}
			},
		},
		methods: {
			async getConfigRecycleBag() {
				const subscribeMpTaskUseOnline = this.subscribeMpTaskUseOnline;
				if (subscribeMpTaskUseOnline) {
					try {
						const {
							data: {
								recycleBag: {
									subscribeMpTaskGotoMode
								},
								taskPlatformBagOutMode: {
									bagOutMode
								}
							}
						} = await getConfigRecycleBag(this, this.bagEqCode);
						this.subscribeMpTask = bagOutMode === 'MP_SUBSCRIBE';
						this.subscribeMpTaskGotoMode = subscribeMpTaskGotoMode;
						console.debug("mp,mode", bagOutMode, subscribeMpTaskGotoMode);
					} finally {
						this.getConfig = true;
					}
				}
			},
			getActivityStatus() {
				console.log("this.bagEqCode:", this.bagEqCode);

				if (this.subscribeMpTaskUseOnline && !this.getConfig) {
					return this.$util.showInfoToast("请稍后，正在初始化");
				}

				if (this.hasSubscribeMpTask) {
					this.$util.doPostForm("/wx/getIsactivity", {
							environmentalProtectionBagMachineCode: this.bagEqCode
						}, {}, true).then(({
							data: {
								code,
								data: res,
								message
							}
						}) => {
							console.debug("getIsActivity:", code, message, JSON.stringify(res));
							const hasTask = code == this.$ResponseStatus.OK;
							this.emitTaskResult(hasTask);
							console.log("hasTask", hasTask);
							if (hasTask) {
								// 兼容之前的版本（updateType是新版才有的参数）
								this.isStatus = true

								if (this.runTaskDirectly) {
									this.authorizationMessage(res);
								}
							} else {
								this.isStatus = false;
								if (this.showErrTip) {
									this.$util.showInfoToast(message ? message : '暂无任务');
								}
								this.emitTaskResult();
							}
						})
						.catch(err => {
							console.error("getIsactivity", err);
							this.emitTaskResult();
						});
				} else {
					this.emitTaskResult();
				}
			},

			emitTaskResult(hasTask = false) {
				this.$emit(`get-task`, hasTask);
			},

			goToIos() {
				var subsMessageReq = plus.ios.newObject("WXSubscribeMsgReq");
				subsMessageReq.plusSetAttribute("scene", "1234"); // 随便一个整数，0-10000以内
				subsMessageReq.plusSetAttribute("templateId", "h0QaI6rFI8z2yuSSlY1s4ah2UANzGwVD4lKmfLXjcV0");
				// subsMessageReq.plusSetAttribute("appId", "wx08bd57edafa2a1b5"); 
				//[WXApi registerApp:WeiXinShareAppID enableMTA:YES];
				var WXApi = plus.ios.importClass("WXApi")
				console.log("install", plus.ios.invoke(WXApi, "isWXAppInstalled"))
				plus.ios.invoke(WXApi, "registerApp:enableMTA:", "wx08bd57edafa2a1b5", "YES")
				const a = plus.ios.invoke(WXApi, "sendReq:", subsMessageReq);
				console.log(a)
			},
			authorizationMessage({
				h5URL
			}) {
				if (!this.isStatus) {
					return
				}

				if (this.subscribeMpTaskGotoMode === 'APPLET_TO_H5_TRANSFER') {
					return launchMiniProgram({
						appId: "gh_72f060acceb3",
						path: `pages/transfer-web-ext?url=${h5URL}`
					});
				}

				if (isIOS()) {
					this.goToIos();
				} else {
					var SubscribeMessage = plus.android.importClass("com.tencent.mm.opensdk.modelbiz.SubscribeMessage");
					console.log(SubscribeMessage)
					var subsMessageReq = new SubscribeMessage.Req();
					subsMessageReq.plusSetAttribute("scene", 1234); // 随便一个整数，0-10000以内
					subsMessageReq.plusSetAttribute("templateID",
						"h0QaI6rFI8z2yuSSlY1s4ah2UANzGwVD4lKmfLXjcV0"); // 在微信开放后台，可以查看自己应用的APPID对应的templateID

					var IWXAPI = plus.android.importClass("com.tencent.mm.opensdk.openapi.IWXAPI");
					var WXAPIFactory = plus.android.importClass("com.tencent.mm.opensdk.openapi.WXAPIFactory");
					var wxapi = WXAPIFactory.createWXAPI(plus.android.runtimeMainActivity().getContext(),
						"wx08bd57edafa2a1b5"); // appid是你自己微信的appid

					let authorization = wxapi.sendReq(subsMessageReq);
					console.log("授权状态", authorization)
				}
			}
		}
	}
</script>

<style lang="scss">
</style>
